<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html lang="zh">
    <%@include file="/lyear/common/head.jsp"%>
<body>

<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <%@include file="/lyear/common/aside.jsp" %>
        <!--End 左侧导航-->

        <!--头部信息-->
        <%@include file="/lyear/common/top.jsp" %>
        <!--End 头部信息-->

        <!--页面主要内容-->
        <main class="lyear-layout-content">

            <div class="container-fluid">

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-toolbar clearfix">
                                <form class="pull-right search-bar" method="get" action="#!" role="form">
                                    <div class="input-group">
                                        <div class="input-group-btn">
                                            <input type="hidden" name="searchKey" id="search_key" value="nickname">
                                            <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                                                昵称 <span h="nickname"></span>
                                            </button>
                                        </div>
<%--                                        <c:if test="${status==null}">--%>
<%--                                            <input type="hidden" name="s" id="status" value="4">--%>
<%--                                        </c:if>--%>
<%--                                        <c:if test="${status!=null}">--%>
<%--                                            <input type="hidden" name="s" id="status" value="${status}">--%>
<%--                                        </c:if>--%>
                                        <input type="text" class="form-control" id="search-value" value="${searchValue}" name="searchValue" placeholder="请输入" onkeydown="selectUserByCondition()">
                                    </div>
                                </form>
                                <div class="toolbar-btn-action">
                                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo"><i class="mdi mdi-plus"></i>新增</button>
                                    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                                        <div class="modal-dialog" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                    <h4 class="modal-title" id="exampleModalLabel">添加评论</h4>
                                                </div>
                                                <div class="modal-body">
                                                    <form>
                                                        <div class="form-group">
                                                            <label for="nickname" class="control-label">昵称</label>
                                                            <input type="text" class="form-control" id="nickname" name="nickname">
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                                            <button type="submit" class="btn btn-primary" onclick="addAuthor()">提交</button>
                                                        </div>
                                                    </form>
                                                </div>

                                            </div>
                                        </div>
                                    </div>
                                    <a class="btn btn-success m-r-5" href="#!"  onclick="activeAuthor()"i class="mdi mdi-check"></i> 启用</a>
                                    <a class="btn btn-warning m-r-5" href="#!" onclick="disabledAuthor()"><i class="mdi mdi-block-helper" ></i> 禁用</a>
                                    <a class="btn btn-danger" href="#!"  onclick="deleteAuthors()"><i class="mdi mdi-window-close" ></i> 删除</a>
                                </div>
                            </div>
                            <div class="card-body">
                                <div class="alert alert-success" role="alert" style="display: none;" id="resSuccessDiv"></div>
                                <div class="alert alert-danger" role="alert" style="display: none;" id="resFailDiv"></div>

                                <div class="table-responsive">
                                    <table class="table table-bordered">
                                        <thead>
                                        <tr>
                                            <th>
                                                <label class="lyear-checkbox checkbox-primary">
                                                    <input type="checkbox" id="check-all" ><span></span>
                                                </label>
                                            </th>
                                            <th>编号</th>
                                            <th>内容</th>
                                            <th>星级</th>
                                            <th>状态
                                                <select style="border: 0;" id="select">
                                                    <c:if test="${status=='0'}">
                                                        <option value ='4'>全部</option>
                                                        <option value ='0' selected >正常</option>
                                                        <option value ='1' >已禁用</option>
                                                        <option value='2' >删除</option>
                                                    </c:if>
                                                    <c:if test="${status=='1'}">
                                                        <option value ='4'>全部</option>
                                                        <option value ='0'  >正常</option>
                                                        <option value ='1' selected>已禁用</option>
                                                        <option value='2' >删除</option>
                                                    </c:if>
                                                    <c:if test="${status=='2'}">
                                                        <option value ='4'>全部</option>
                                                        <option value ='0'  >正常</option>
                                                        <option value ='1' >已禁用</option>
                                                        <option value='2' selected>删除</option>
                                                    </c:if>
                                                    <c:if test="${status==null||status=='4'}">
                                                        <option value ='4' selected>全部</option>
                                                        <option value ='0'  >正常</option>
                                                        <option value ='1' >已禁用</option>
                                                        <option value='2' >删除</option>
                                                    </c:if>
                                                </select>
                                            </th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <c:forEach items="${pageBean.result}" var="comment">
                                            <tr>
                                                <td>
                                                    <label class="lyear-checkbox checkbox-primary">
                                                        <input type="checkbox" name="ids[]" value="${comment.id}"><span></span>
                                                        <input type="checkbox" name="status[]" value="${comment.status}">
                                                    </label>
                                                </td>
                                                <td>${comment.id}</td>
                                                <td>${comment.info}</td>
                                                <td>${comment.stars}</td>
<%--                                                <td>--%>
<%--                                                    <img src="${user.avatar}" height="100px" width="100px">--%>
<%--                                                </td>--%>
                                                <td>
                                                        <%--     0:正常,1:禁用,2:删除--%>
                                                    <c:if test="${comment.status==0}">
                                                        <span style="color: #0FB25F;">正常</span>
                                                    </c:if>
                                                    <c:if test="${comment.status==1}">
                                                        <span style="color: yellow;">禁用</span>
                                                    </c:if>
                                                    <c:if test="${comment.status==2}">
                                                        <span style="color: red;">删除</span>
                                                    </c:if>
                                                </td>
                                                <td>
                                                    <div class="btn-group">
                                                        <a class="btn btn-xs btn-default" href="${pageContext.request.contextPath}/author/getByIdEdit/${comment.id}" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                                                        <a class="btn btn-xs btn-default" href="${pageContext.request.contextPath}/author/getById/${comment.id}" title="查看" data-toggle="tooltip"><i class="mdi mdi-eye"></i></a>
                                                        <a class="btn btn-xs btn-default" href="#!" title="删除" data-toggle="tooltip">
                                                            <i class="mdi mdi-window-close" onclick="deleteAuthor(${comment.id},${comment.status})"></i>
                                                        </a>
                                                    </div>
                                                </td>
                                            </tr>
                                        </c:forEach>
                                        </tbody>
                                    </table>
                                </div>

                                <%-- 分页--%>
                                <%@include file="/lyear/common/page.jsp" %>
                                <!--End 分页-->

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>

<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/main.min.js"></script>
<!--对话框-->
<script src="${pageContext.request.contextPath}/lyear/js/jconfirm/jquery-confirm.min.js"></script>

<script type="text/javascript">

    function deleteAuthor(userId,status) {
        if (status==2){
            $.confirm({
                title: '温馨提示',
                content: '该作者已经被删除',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        $.confirm({
            title: '删除作者',
            content: '确认删除吗？',
            type:'red',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function () {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/author/deleteAuthor/" + userId,
                            type: "DELETE",
                            success: function (res) {
                                if (res.code == 200) {
                                    $("#resSuccessDiv").css("display", "block");
                                    $("#resSuccessDiv").text(res.msg).delay(3000).hide(300);
                                    setTimeout("location.reload()",3000);
                                }
                                if (res.code == 500) {
                                    $("#resFailDiv").css("display", "block");
                                    $("#resFailDiv").text(res.msg);
                                }

                            },
                            dataType: 'json'
                        })

                    }

                },
                cancel: {
                    text: '取消'
                }
            }
        });
    };


    function deleteAuthors(){
        let userId=[];
        let status=[];
        $("input[name='ids[]']:checked").each(function(){
            userId.push($(this).val());
            status.push($(this).next().next().val());
        })
        let a=0;
        for (let i = 0; i < status.length; i++) {
            if (status[i]!=1){
                a++;
            }
        }
        if (a!=0){
            $.confirm({
                title: '温馨提示',
                content: '作者已经被删除',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        if (userId.length==0){
            $.confirm({
                title: '温馨提示',
                content: '请添加需要删除的作者',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        $.confirm({
            title: '删除作者',
            content: '确认删除吗？',
            type:'red',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function () {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/author/deleteAuthors/" + userId,
                            type: "DELETE",
                            success: function (res) {
                                if (res.code == 200) {
                                    $("#resSuccessDiv").css("display", "block");
                                    $("#resSuccessDiv").text(res.msg).delay(3000).hide(300);
                                    setTimeout("location.reload()",3000);
                                }
                                if (res.code == 500) {
                                    $("#resFailDiv").css("display", "block");
                                    $("#resFailDiv").text(res.msg);
                                }
                            },
                            dataType: 'json'
                        })
                    }
                },
                cancel: {
                    text: '取消'
                }
            }
        });
    };
    /**
     * 模糊查找
     */
    function selectUserByCondition(e) {
        let evt = window.event || e;
        if (evt.keyCode == 13) { //回车事件
            $.get("${pageContext.request.contextPath}/author/getAuthorByCondition/1/10");
        }
    }

    /**
     * 添加作者
     */
    function addAuthor(){
        let nicknameRes = $("#nickname").val();
        $.ajax({
            url: "${pageContext.request.contextPath}/author/addAuthor",
            data:{"nickname":nicknameRes},
            type: "POST",
            success: function (res) {
                $("#close").click();
                if (res.code == 200) {
                    $("#resSuccessDiv").css("display", "block");
                    $("#resSuccessDiv").text(res.msg).delay(3000).hide(300);
                }else {
                    $("#resFailDiv").css("display", "block");
                    $("#resFailDiv").text(res.msg);
                }
                setTimeout(function(){
                    location.reload();
                },5000);
            }
        })
    }
    $("#select").on('change',function(){
        var url = "/projshow/author/getAuthorByCondition/1/10";
        let status = $(this).val();
        let searchKey = $('#search_key').val();
        let searchValue = $('#search-value').val();

        location.href="/projshow/author/getAuthorByCondition/1/10?searchKey="+searchKey+"&searchValue"+searchValue+"&s="+status;

    })
    function activeAuthor(){
        let authorId=[];
        let status=[];
        $("input[name='ids[]']:checked").each(function(){
            authorId.push($(this).val());
            status.push($(this).next().next().val());
        })
        let a=0;
        for (let i = 0; i < status.length; i++) {
            if (status[i]!=1){
                a++;
            }
        }
        if (a!=0){
            $.confirm({
                title: '温馨提示',
                content: '请选择已经禁用的作者',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        if (authorId.length==0){
            $.confirm({
                title: '温馨提示',
                content: '请选择需要启用的作者',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        $.confirm({
            title: '启用作者',
            content: '确认启用吗？',
            type: 'red',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function () {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/author/activeById/" + authorId,
                            type:"POST",
                            success: function (res) {
                                if (res.code == 200) {
                                    $("#resSuccessDiv").css("display", "block");
                                    $("#resSuccessDiv").text(res.msg).delay(3000).hide(300);
                                    setTimeout("location.reload()",3000);
                                }
                                if (res.code == 500) {
                                    $("#resFailDiv").css("display", "block");
                                    $("#resFailDiv").text(res.msg);
                                }
                            },
                            dataType: 'json'
                        })
                    }
                },
                cancel: {
                    text: '取消'
                }
            }
        });
    };

    function disabledAuthor(){
        let technologyId=[];
        let status=[];
        $("input[name='ids[]']:checked").each(function(){
            technologyId.push($(this).val());
            status.push($(this).next().next().val());
        })
        let a=0;
        for (let i = 0; i < status.length; i++) {
            if (status[i]!=0){
                a++;
            }
        }
        if (a!=0){
            $.confirm({
                title: '温馨提示',
                content: '请选择正常状态下的作者',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        if (technologyId.length==0){
            $.confirm({
                title: '温馨提示',
                content: '请选择需要禁用的作者',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
        $.confirm({
            title: '禁用作者',
            content: '确认禁用吗？',
            type: 'red',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function () {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/author/disabledById/" + technologyId,
                            type:"POST",
                            success: function (res) {
                                if (res.code == 200) {
                                    $("#resSuccessDiv").css("display", "block");
                                    $("#resSuccessDiv").text(res.msg).delay(3000).hide(300);
                                    setTimeout("location.reload()",3000);
                                }
                                if (res.code == 500) {
                                    $("#resFailDiv").css("display", "block");
                                    $("#resFailDiv").text(res.msg);
                                }
                            },
                            dataType: 'json'
                        })
                    }
                },
                cancel: {
                    text: '取消'
                }
            }
        });
    };
</script>
</body>
</html>